//
//  ChangePasswrodViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-7-27.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "NetworkAccessManager.h"
#import "UIAlertView+Kirinrin.h"
#import <UIActivityIndicatorView+AFNetworking.h>
#import <TSMessage.h>
#import "Keychain.h"
#import "UtilHelper.h"

@interface ChangePasswordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTextField;
@property (weak, nonatomic) IBOutlet UITextField *nnewPasswordTextField;

@property (weak, nonatomic) IBOutlet UITextField *confrimTextField;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *waiter;

@end


@implementation ChangePasswordViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)doneAction:(id)sender {
    [self.view endEditing:YES];
    if ([_oldPasswordTextField.text isEqualToString:@""]||[_nnewPasswordTextField.text isEqualToString:@""]) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildLength];
        return;
    }
    if (![_nnewPasswordTextField.text isEqualToString:_confrimTextField.text]) {
        [UIAlertView alertWithFormCheckError:FCErrorInvaildPassword];
        return;
    }
    
    
    
    NSString *uriString = [NSString stringWithFormat:@"Account/%@/changePassword.json", self.appDelegate.userId];
    NSString *md5OldPassword = [UtilHelper md5LowCase:_oldPasswordTextField.text];
    NSString *md5NewPassword = [UtilHelper md5LowCase:_nnewPasswordTextField.text];
    
    NSDictionary *parameters = @{@"key": self.appDelegate.accessKey, @"oldPassword":md5OldPassword, @"newPassword":md5NewPassword};
    
    
    AFHTTPRequestOperation *operation = [[NetworkAccessManager shareManager]POST:uriString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"opernation: %@\nresponse: %@", operation, responseObject);
        
        int resultCode = [responseObject[@"result"]intValue];
        if (resultCode == 0) {
            
            NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[Keychain load:KEY_USERNAME_PASSWORD];
            
            NSDictionary *data = @{KEY_USERNAME: usernamepasswordKVPairs[KEY_USERNAME], KEY_PASSWORD: _nnewPasswordTextField.text};
            [Keychain save:KEY_USERNAME_PASSWORD data:data];

            [TSMessage showNotificationWithTitle:@"操作成功" subtitle:@"密码已成功修改" type:TSMessageNotificationTypeSuccess];
            [self.navigationController popViewControllerAnimated:YES];
            
            
        }else{
            [UIAlertView alertWithRemoteError:resultCode];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"opernation: %@\nerror: %@", operation, error);
        [UIAlertView alertWithNetworkError];
    }];

    [_waiter setAnimatingWithStateOfOperation:operation];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == _oldPasswordTextField) {
        [_nnewPasswordTextField becomeFirstResponder];
    }
    if (textField == _nnewPasswordTextField) {
        [_confrimTextField becomeFirstResponder];
    }
    if (textField == _confrimTextField) {
        [self doneAction:textField];
    }
    
    return YES;
}
@end
